package com.talkweb.newproject.admin.controller;

import com.talkweb.newproject.admin.rest.SysResourceRest;
import com.talkweb.newproject.admin.sysresource.dto.SysResourceDTO;
import com.talkweb.newproject.api.model.admin.SysResourceParam;
import com.talkweb.newproject.api.model.admin.TreeNodeVo;
import com.talkweb.newproject.api.model.common.ResponseResult;
import com.talkweb.newproject.api.model.common.Status;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by qsf on 2019/5/13.
 */
@RestController
@RequestMapping(value="/sysResource_api")
@Slf4j
public class SysResourceController {
    @Autowired
    private SysResourceRest sysResourceRest;

    /**
     * 根据id查询resource信息
     * @param ID
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/querySysResourceByPrimaryKey",method = RequestMethod.POST)
    public ResponseResult<SysResourceDTO> querySysResourceByPrimaryKey(@RequestParam("ID") String ID) throws Exception {
        ResponseResult<SysResourceDTO> result = new ResponseResult<>();
        try{
            return sysResourceRest.querySysResourceByPrimaryKey(ID);

        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }

    }

    @ResponseBody
    @RequestMapping(value="/getSysResourceList",method = RequestMethod.POST)
    public ResponseResult<List<TreeNodeVo>> querySysResourceList(@RequestBody SysResourceParam obj) throws Exception {
        ResponseResult<List<SysResourceDTO>> sysResourceList = new ResponseResult<>();
        ResponseResult<List<TreeNodeVo>> result = new ResponseResult<>();
        try{
            sysResourceList = sysResourceRest.querySysResourceList(obj);
            result.setData(this.toTreeModel(sysResourceList.getData()));
            result.setStatus(sysResourceList.getStatus());
            return result;
        }catch(Exception e) {
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    private List<TreeNodeVo> toTreeModel(List<SysResourceDTO> data) {
        List<TreeNodeVo> treeNodeList = new ArrayList<>();
        for(SysResourceDTO dto: data) {
            TreeNodeVo vo = new TreeNodeVo();
            vo.setId(dto.getId().toString());
            vo.setName(dto.getResourceName());
            vo.setParentId(dto.getParentId());
            treeNodeList.add(vo);
        }
        return treeNodeList;
    }

    /**
     * 根据条件查询对象
     * @param obj
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/queryLike",method = RequestMethod.POST)
    public ResponseResult<SysResourceDTO> queryLike(@RequestBody SysResourceDTO obj) throws Exception {
        ResponseResult<SysResourceDTO> result = new ResponseResult<>();
        try{
            return sysResourceRest.queryLike(obj);
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    @ResponseBody
    @RequestMapping(value="/saveSysResource",method = RequestMethod.POST)
    public ResponseResult insertSysResource(@RequestBody SysResourceDTO obj) throws Exception {
        ResponseResult result= new ResponseResult();
        try{
            obj.setValidateState("1");
            obj.setVersion((long)1);
            if(obj!= null && obj.getId()>0) {
                return sysResourceRest.update(obj);
            }else {
                return sysResourceRest.create(obj);
            }
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }

    }

    @ResponseBody
    @RequestMapping(value="/Web/update/v1",method = RequestMethod.POST)
    public ResponseResult updateSysResource(@RequestBody SysResourceDTO obj) throws Exception {
        ResponseResult result = new ResponseResult();
        try{
            return sysResourceRest.update(obj);

        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    @ResponseBody
    @RequestMapping(value="/deleteSysResourceByID",method = RequestMethod.POST)
    public ResponseResult deleteSysResourceByID(@RequestParam("") String orgid) throws Exception {
        ResponseResult result = new ResponseResult();
        try{

            return sysResourceRest.deleteResourceById(orgid);

        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result; 
        }

    }
}
